﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.IO;
using System.Xml;

namespace AJRavindiran.Jolt.Utilities.Parsing
{
    /// <summary>
    /// Represents a xml parser.
    /// </summary>
    public class Xml
    {
        #region Properties
        /// <summary>
        /// The file to read off.
        /// </summary>
        public string File { get; set; }
        #endregion Properties

        #region Constructors
        /// <summary>
        /// Constructs a xml class.
        /// </summary>
        /// <param name="file"></param>
        public Xml(string file)
        {
            this.File = file;
        }
        #endregion Constructors

        #region Methods
        /// <summary>
        /// Change the currently loaded file to a new file, incase 
        /// we need to load something from somewhere else.
        /// </summary>
        public void ChangeFile(string file)
        {
            this.File = file;
        }

        /// <summary>
        /// Read the data within the given node.
        /// </summary>
        private string Read(string key)
        {
            XmlNodeList xData = null;
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(Utilities.PathUtilities.GetConfigPath(File));
                xData = xDoc.GetElementsByTagName(key);
            }
            catch (Exception ex)
            {
                Jolt.GetLog().WriteException(ex);
            }
            return xData[0].InnerText;
        }

        /// <summary>
        /// Read the data as a boolean.
        /// </summary>
        public bool ReadBoolean(string key)
        {
            return bool.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a byte
        /// </summary>
        public byte ReadByte(string key)
        {
            return byte.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a char
        /// </summary>
        public char ReadChar(string key)
        {
            return char.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a decimal
        /// </summary>
        public decimal ReadDecimal(string key)
        {
            return decimal.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a double
        /// </summary>
        public double ReadDouble(string key)
        {
            return double.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a short
        /// </summary>
        public short ReadInt16(string key)
        {
            return short.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a int
        /// </summary>
        public int ReadInt32(string key)
        {
            return int.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a long
        /// </summary>
        public long ReadInt64(string key)
        {
            return long.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a sbyte
        /// </summary>
        public sbyte ReadSByte(string key)
        {
            return sbyte.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a single
        /// </summary>
        public Single ReadSingle(string key)
        {
            return Single.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a string
        /// </summary>
        public string ReadString(string key)
        {
            return Read(key);
        }

        /// <summary>
        /// Read the data as a ushort
        /// </summary>
        public ushort ReadUInt16(string key)
        {
            return ushort.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a uint
        /// </summary>
        public uint ReadUInt32(string key)
        {
            return uint.Parse(Read(key));
        }

        /// <summary>
        /// Read the data as a ulong
        /// </summary>
        public ulong ReadUInt64(string key)
        {
            return ulong.Parse(Read(key));
        }
        #endregion Methods
    }
}
